package com.gzist.elderlyserviceplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzist.elderlyserviceplatform.common.base.BaseDTO;
import com.gzist.elderlyserviceplatform.entity.dto.VideoDto;
import com.gzist.elderlyserviceplatform.entity.po.Video;
import com.gzist.elderlyserviceplatform.mapper.ActivityMapper;
import com.gzist.elderlyserviceplatform.mapper.VideoMapper;
import com.gzist.elderlyserviceplatform.service.ElderlyInfoService;
import com.gzist.elderlyserviceplatform.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service

public class VideoServiceImpl extends ServiceImpl<VideoMapper,Video> implements VideoService {
    @Autowired
    private ElderlyInfoService elderlyInfoService;
    @Override
    public Page<Video> ClientlistVideo(BaseDTO baseDTO) {
        //如果是第一页，则修改返回的数据
        if (baseDTO.getPageNum() == 1) {


            Page<Video> page =this.page(new Page<Video>(baseDTO.getPageNum(), baseDTO.getPageSize()));
            List<Video> recommendedList = this.recommendedLists();
            // 第一页时融合推荐数据逻辑
            page.setRecords(recommendedList);
            return page;
        }
        return this.page(new Page<Video>(baseDTO.getPageNum(), baseDTO.getPageSize()));
    }

    @Override
    public Page<Video> queryList(VideoDto videoDto) {
        LambdaQueryWrapper<Video> wrapper =new LambdaQueryWrapper<>();
        wrapper.like(Video::getTitle,videoDto.getTitle());
        return this.page(new Page<Video>(videoDto.getPageNum(), videoDto.getPageSize()),wrapper);
    }

    @Override
    public Boolean update(Video video) {
        LambdaUpdateWrapper<Video> wrapper =new LambdaUpdateWrapper<>();
                wrapper.eq(Video::getId,video.getId());
                return this.update(video,wrapper);
    }

    @Override
    //推荐算法
    public List<Video> recommendedLists() {

        LambdaQueryWrapper<Video> wrapper =new LambdaQueryWrapper<>();
        //获取平均值
        Integer averageAge = elderlyInfoService.getAverageAge();
        //获得区间
        int max = averageAge+20;
        int min = averageAge-20;

        //获取区间内数据
        wrapper.ge(Video::getMinAge,min )
                .le(Video::getMaxAge, max)
                .eq(Video::getDelFlag, 0)
                .last("limit 8");
        List<Video> videos = this.list(wrapper);

        //如果数据不够，则补充，获取区间外数据
        if (videos.size() < 8){
            LambdaQueryWrapper<Video> wrapper1 =new LambdaQueryWrapper<>();
            wrapper1.le(Video::getMinAge, min)
                    .or().ge(Video::getMaxAge, max)
                    .eq(Video::getDelFlag, 0)
                    .last("limit " + (8 - videos.size()));
            List<Video> videos1 = this.list(wrapper1);
            for (Video video : videos1) videos.add(video);
        }
        return videos;
    }
}
